let common = require("../../utils/common.js")
let util = require("../../utils/util.js")
Page({
  data: {
    hosp: '选择医院',
    hospId: '',
    ishospshow: false,
    hospcolumns: [],
    hosplv: '全部',
    hosplvId: 0,
    ishosplvshow: false,
    hosplvcolumns: [{
      id: 0,
      text: '全部'
    }, {
      id: 1,
      text: '一级'
    }, {
      id: 2,
      text: '二级'
    }, {
      id: 3,
      text: '三级'
    }],
    latitude: '',
    longitude: '',
    markers: [],
  },
  onLoad: function (options) {
    this.setLocation()
    common.initHosp(this)
    this.initCurrentHosp('', '')
  },
  initCurrentHosp: function (bedNum, addr) {
    let self = this
    let iData = wx.getStorageSync('idata')
    if (iData) {
      let hosp = iData.hosp
      if (hosp.length > 0) {
        let cur = hosp[0]
        for (let i = 0; i < hosp.length; i++) {
          if (hosp[i].ikey == self.data.hospId && self.data.hospId != 0) {
            cur = hosp[i]
          } else if (hosp[i].dj == self.data.hosplv && self.data.hosplv != 0) {
            cur = hosp[i]
          } else if (hosp[i].bn == bedNum && !util.isEmpty(bedNum)) {
            cur = hosp[i]
          } else if (hosp[i].addr == addr && !util.isEmpty(addr))
            cur = hosp[i]
        }
        if (cur) {
          self.setMarker(cur)
        }
      }
    }
  },
  setLocation: function () {
    let self = this
    wx.getSetting({
      success(res) {
        //查看是否授权获取地理信息
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              self.getLocation();
            },
            fail() {
              wx.showToast({
                title: '定位失败',
              })
            }
          })
        } else {
          self.getLocation();
        }
      }
    })
  },
  getLocation: function () {
    var self = this;
    wx.showLoading({
      title: '正在定位中...',
      mask: true
    })
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        console.log(res)
        //当前定位点
        self.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        })
      },
      fail(err) {
        wx.showToast({
          title: '定位失败',
          icon: 'none'
        })
      },
      complete() {
        wx.hideLoading()
      }
    })
  },
  showhosptap: function () {
    this.setData({
      ishospshow: true
    })
  },
  onhospcancel: function () {
    this.setData({
      ishospshow: false
    })
  },
  onhospconfirm: function (e) {
    var value = e.detail.value
    this.setData({
      hosp: value.text,
      hospId: value.id,
      ishospshow: false
    })
  },
  showhosplvtap: function () {
    this.setData({
      ishosplvshow: true
    })
  },
  onhosplvcancel: function () {
    this.setData({
      ishosplvshow: false
    })
  },
  onhosplvconfirm: function (e) {
    var value = e.detail.value
    this.setData({
      hosplv: value.text,
      hosplvId: value.id,
      ishosplvshow: false
    })
  },
  formsubmit: function (e) {
    console.log(e)
    let self = this
    let detail = e.detail.value
    let bednum = detail.bednum
    let addr = detail.addr
    self.initCurrentHosp(bednum, addr)
  },
  setMarker: function (data) {
    let self = this
    let marker = {}
    let markers = new Array()
    if (!util.isEmpty(data.lat) && !util.isEmpty(data.lng)) {
      marker.id = data.ikey
      marker.title = data.hosp
      marker.iconPath = '../../images/pos.png'
      marker.width = '50rpx'
      marker.height = '50rpx'
      marker.latitude = data.lat
      marker.longitude = data.lng
      markers.push(marker);
    }
    self.setData({
      markers: markers
    })
  }
})